import config from "@/config.js";
import request from "@/utils/request"
import methods from "@/utils/methods.js";
export default {
	data() {
		return {
			config: config, //配置文件
			// #ifdef MP-WEIXIN
			menuButtonInfo: uni.getMenuButtonBoundingClientRect(),
			statusBarHeigh: uni.getSystemInfoSync().statusBarHeight,
			safeAreaBottom: uni.getSystemInfoSync(),
			// #endif
			// #ifdef H5
			menuButtonInfo: {
				height: '0'
			},
			statusBarHeigh: '0',
			safeAreaBottom: {
				safeAreaInsets: {
					bottom: '0'
				}
			},
			// #endif
			cosUrl: 'https://gaokao.s3.cn-north-1.jdcloud-oss.com/',
			request: request,
			methods: methods,
			showVip: false, //隐藏变量
			noticeflag: {
				Show: false, //个人中心登录弹窗
				flag: '',
			}

		}
	},
	filters: {
		// 南宁、柳州、桂林、钦州
		formatCity(cityName) {
			switch (cityName) {
				case 'c':
					return '冲';
				case 'z':
					return '争';
				case 'w':
					return '稳';
				case 's':
					return '守';
				default:
					return '全部';
			}
		}
	},
	//全局分享朋友方法
	// #ifdef MP-WEIXIN
	onShareAppMessage: res => {},
	// #endif
	methods: {
		init() {
			this.noticeflag.Show = true
			this.noticeflag.flag = 'index'
		},
		navswitch(url) {
			uni.switchTab({
				url: url
			})
		},
		redirectTo(url) {
			uni.redirectTo({
				url: url
			})
		},
		reLaunch(url) {
			uni.reLaunch({
				url: url
			})
		},
		removeSpanTags(str) {
			return str.replace(/<span[^>]*>(.*?)<\/span>/g, '\$1');
		},
		joinValues(arr, key) {
			// 使用 map 提取数组对象中的特定字段值，使用 join 进行拼接
			return arr.map(item => item[key]).join(',');
		},

		navgateTo(url, param) {
			let ext = '';
			if (param) {
				for (let key in param) {
					if (ext) {
						ext = ext + '&' + key + '=' + param[key];
					} else {
						ext = key + '=' + param[key];
					}
				}
			}
			if (ext) {
				url += '?' + ext;
			}
			uni.navigateTo({
				url: url
			})
		},
		goback(delta = 1) {
			uni.navigateBack({
				delta: delta,
				fail: err => {
					this.navLaunch('/pages/index/index');
				}
			})
		},
		//数组转字符
		arrayToStr(arr, mark = ',') {
			console.log('str', arr.join(mark))
			return arr.join(mark);
		},
		strToArr(str = '') {
			return str.split(" ")
		},
		mapArray(arr1, arr2) {
			  let  resultArray = [...arr1]
			  arr2.forEach(item2 => {
			    let existingItem = resultArray.find(item1 => item1.scode == item2.scode);
			    if (!existingItem) {
			      resultArray.push(item2);
			    } else {
			      const index = resultArray.findIndex(item => item.scode == item2.scode);
			      resultArray[index] = item2; 
			    }
			  });
			  return resultArray;
		},
		mapArrName(arr1, arr2) {
			  let  resultArray = [...arr1]
			  arr2.forEach(item2 => {
			    let existingItem = resultArray.find(item1 => item1.name == item2.name);
			    if (!existingItem) {
			      resultArray.push(item2);
			    } else {
			      const index = resultArray.findIndex(item => item.name == item2.name);
			      resultArray[index] = item2; 
			    }
			  });
			  return resultArray;
		},
		strToarray(str, separator = ',') {
			const arr = str.split(separator);
			// 2. 转换为数组对象，可以根据需求扩展此部分
			const arrObj = arr.map((item, index) => ({
				id: index + 1,
				name: item.trim() // 去除两边的空格
			}));
			return arrObj;
		},
		saveData: (key, value) => {
			return uni.setStorageSync(key, value);
		},

		deleteData: (key) => {
			uni.removeStorageSync(key);
		},

		mitprovince(arr, key) {
			return arr.map(item => item[key]).join(','); // 提取数组中每个对象的key值
		},
		copyText(text = '') {
			uni.setClipboardData({
				data: text,
				success: res => {
					this.showMsg('复制成功')
				}
			})
		},
		//判断手机号是否正确
		checkPhone(phone) {
			const paramType = typeof phone;
			if (paramType !== 'number' && paramType !== 'string') return {
				pass: false,
				msg: '手机号码数据类型不正确'
			}
			if (paramType === 'number') phone = `${phone}`;
			if (!phone) return {
				pass: false,
				msg: '请输入手机号码'
			};
			const reg = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
			const pass = reg.test(phone);
			if (!pass) return {
				pass: false,
				msg: '手机号码无效，请输入正确的手机号码'
			};
			return {
				pass: true
			}
		},
		showloading(text = '加载中') {
			uni.showLoading({
				title: text,
				mask: true
			})
		},
		showMsg: (text, callback = '') => {
			uni.showToast({
				title: text ? text : '',
				icon: 'none',
				duration: 3000
			});
			setTimeout(function() {
				callback && callback(1);
			}, 3000);
		},
		//操作成功后，的提示信息
		successToShow(msg = '保存成功', callback) {
			setTimeout(function() {
				uni.showToast({
					title: msg,
					icon: 'success',
					duration: 2000,
					success() {
						setTimeout(function() {
							callback()
						}, 2000)
					}
				});
			}, 100)
		},
		//操作失败的提示信息
		errorToShow(msg = '操作失败', callback = function() {}) {
			setTimeout(function() {
				uni.showToast({
					title: msg,
					icon: 'none',
					duration: 2000,
					success() {
						setTimeout(function() {
							callback()
						}, 2000)
					}
				})
			}, 2000)
		},
		cosImageUrl(img = '', cos = true) {
			return cos ? this.cosUrl + img : '/static' + img
		},
		parseplan(arr) {
			const newArray = [];
			arr.forEach(item => {
				if (item.obj) {
					const newObj = `${item.obj.majorPlan}`;
					newArray.push(newObj);
				}
			});
			return newArray.join(',');
		},
		// 封装方法，接受多个对象
		parseObjects(arr) {
			// 用来存储处理后的新数组
			const newArray = [];
			arr.forEach(item => {
				// 判断每一个对象是否包含obj字段
				if (item.obj) {
					console.log('item.obj', item.obj)
					// 提取三个指定的值（假设你需要的字段是 obj.key1, obj.key2, obj.key3）
					const newObj = [
						item.name == null ? 'null' : item.name,
						item.obj.majorPm == null ? "null" : item.obj.majorPm,
						item.obj.majorPlan == null ? "null" : item.obj.majorPlan,
						item.obj.submitRate == null ? "null" : item.obj.submitRate,
					].filter(Boolean).join(',');
					// 将新的值用分号分隔后存入新数组
					newArray.push(newObj);
				}
			});
			return newArray.join(';');
		},
		clearData: methods.clearData,
		navLaunch: methods.navLaunch,
		readData: methods.readData,
		processArray: methods.processArray,
		processreArr: methods.processreArr,
		processryear: methods.processryear,
		filprovince: methods.province,
		processzxkm: methods.processzxkm,
	}
}